﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Membership;

namespace Ue.Qcdz.UI.Controls.Membership
{
    public class UserRegisterEdit : SkinnedControl
    {
        private string skinFileName = "~/Themes/zh-cn/Skins/UserRegisterEdit.ascx";
        private Label lblUserName;
        private TextBox tbxName;
        private TextBox tbxBirthDay;
        private TextBox tbxTelNum;
        private TextBox tbxAreaNum;
        private TextBox tbxMobile;
        private TextBox tbxEmail;
        private TextBox tbxQQnumber;
        private TextBox tbxPostalCode;
        private TextBox tbxUserAddres;
        private SexRadio sexDropdown;
        private Button btnSubmit;
        private User user;
        private IUserRepository repository;
        protected override string GetSkinFileName()
        {
            return skinFileName;
        }
        private void GetInfo(string strUserName)
        {
             repository =
                Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
              user = repository.FindBy(HttpContext.Current.User.Identity.Name);
             lblUserName.Text = user.UserName.Split('@')[0];
             tbxBirthDay.SetValue(user.Profile.Birthday);
             tbxName.SetValue(user.Profile.Name);
             tbxMobile.SetValue(user.Profile.MobilePhone);
             if (!string.IsNullOrEmpty(user.Profile.Telephone))
             {
                 tbxAreaNum.SetValue(user.Profile.Telephone.Split('-')[0]);
                 tbxTelNum.SetValue(user.Profile.Telephone.Split('-')[1]);
             }

             tbxEmail.SetValue(user.Profile.Email);
             tbxQQnumber.SetValue(user.Profile.QQnumber);
             tbxPostalCode.SetValue(user.Profile.PostalCode);
             tbxUserAddres.SetValue(user.Profile.UserAddress);
             sexDropdown.SelectedValue = (SexType)user.Profile.Sex;
        }

        protected override void InitializeSkin(Control skin)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                lblUserName = skin.FindControl<Label>("lblUserName");
                sexDropdown = skin.FindControl<SexRadio>("sexDropdown");
                tbxBirthDay = skin.FindControl<TextBox>("tbxBirthDay");
                tbxName = skin.FindControl<TextBox>("tbxName");
                tbxTelNum = skin.FindControl<TextBox>("tbxTelNum");
                tbxAreaNum = skin.FindControl<TextBox>("tbxAreaNum");
                tbxMobile = skin.FindControl<TextBox>("tbxMobile");
                tbxEmail = skin.FindControl<TextBox>("tbxEmail");
                tbxQQnumber = skin.FindControl<TextBox>("tbxQQnumber");
                tbxPostalCode = skin.FindControl<TextBox>("tbxPostalCode");
                tbxUserAddres = skin.FindControl<TextBox>("TbxAddress");
                btnSubmit = skin.FindControl<Button>("btnSubmit");
                btnSubmit.Click += new EventHandler(btnSubmit_Click);
                GetInfo(HttpContext.Current.User.Identity.Name);
            }
            else
            {
                MessageWindow.RenderMessage("请您先登录", "~/login.aspx");
            }
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            user.Profile.Birthday = tbxBirthDay.GetValue<string>();
            user.Profile.Name = tbxName.GetValue<string>();
            user.Profile.Sex = (int)sexDropdown.SelectedValue;
            user.Profile.MobilePhone = tbxMobile.GetValue<string>();
            string strAreaNum = tbxAreaNum.GetValue<string>();
            string strTel = tbxTelNum.GetValue<string>();
            user.Profile.Telephone = GetTel(strAreaNum, strTel);
            user.Profile.Email = tbxEmail.GetValue<string>();
            user.Profile.QQnumber = tbxTelNum.GetValue<string>();
            user.Profile.PostalCode = tbxPostalCode.GetValue<string>();
            user.Profile.UserAddress = tbxUserAddres.GetValue<string>();
            repository[user.Key] = user;
            MessageWindow.RenderMessage("修改成功");
        }

        private string GetTel(string strAreaCode, string strTelNum)
        {
            if (!string.IsNullOrEmpty(strTelNum))
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(strAreaCode);
                stringBuilder.Append("-");
                stringBuilder.Append(strTelNum);
                return stringBuilder.ToString();
            }
            return "";
        }
       
    }
}
